package com.ly.cloud.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ly.cloud.dto.CampusBasicInfoDto;
import com.ly.cloud.entity.CampusBasicInfoPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.CampusBasicInfoMapper;
import com.ly.cloud.service.CampusBasicInfoService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.CampusBasicInfoTyVo;
import com.ly.cloud.vo.CampusBasicInfoVo;

@Service
public class CampusBasicInfoServiceImpl implements CampusBasicInfoService {

	@Autowired
	private CampusBasicInfoMapper campusBasicInfoMapper;
	/** 
	 * Method Name:  insertCampusInfo
	 * Description:  
	 * 添加校区信息
	 * @param dto 
	 * @param createMan 创建人
	 * @author LuHuaCong
	 * @date: 2018年12月11日
	 */
	@Override
	public boolean insertCampusInfo(CampusBasicInfoDto dto,String createMan) {
		
		try {
			CampusBasicInfoPo po = new CampusBasicInfoPo();
			BeanUtils.copyProperties(dto, po);
			po.setCjr(createMan);
			po.setCjsj(new Date());
			Integer i = campusBasicInfoMapper.insert(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			throw new DBException("添加校区失败！", e);
		}
	}
	
	/** 
	 * Method Name:  updateCampusInfo
	 * Description:  
	 * 修改校区基本信息
	 * @param dto
	 * @param modifuMan 修改人
	 * @return boolean	是否修改成功
	 * @author luhuacong
	 * @date: 2018年12月13日
	 */
	@Override
	public boolean updateCampusInfo(CampusBasicInfoDto dto, String modifuMan) {
		
		try {
			CampusBasicInfoPo po = new CampusBasicInfoPo();
			BeanUtils.copyProperties(dto, po);
			po.setXgr(modifuMan);
			po.setXgsj(new Date());
			Integer i = campusBasicInfoMapper.updateById(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			throw new DBException("修改校区信息失败！", e);
		}
	}

	/** 
	 * Method Name:  deleteById
	 * Description:  
	 * 根据id删除校区基本信息
	 * @param id
	 * @return boolean	是否删除成功
	 * @author luhuacong
	 * @date: 2018年12月13日
	 */
	@Override
	public boolean deleteById(String id) {
		try {
			Integer i = campusBasicInfoMapper.deleteById(id);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("根据id删除校区信息失败!", e);
		}
	}

	/** 
	 * Method Name:  selectCampusList
	 * Description:  
	 * 查询学校校区
	 * @param xqmc 校区名称
	 * @return List<CampusBasicInfoVo>
	 * @author luhuacong
	 * @date: 2018年12月13日
	 */
	@Override
	public List<CampusBasicInfoVo> selectCampusList(String xqmc) {
		try {
			EntityWrapper<CampusBasicInfoPo> ew = new EntityWrapper<>();
			ew.like("XQMC", xqmc);
			ew.orderBy("XQMC", true);
			List<CampusBasicInfoPo> list = campusBasicInfoMapper.selectList(ew);
			return BeanCopyUtils.copyListProperties(list, CampusBasicInfoVo.class);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("查询校区失败！", e);
		}
	}

	/** 
	 * Method Name:  selectCampusList
	 * Description:  
	 *查询学校校区，用于下拉列表。若jxlmc不为空，则根据教学楼号查询拥有该教学楼名称的校区
	 * 					  若jxlmc为空，则查询全部校区
	 * @Param jxlmc 教学楼名称
	 * @return List<CampusBasicInfoTyVo>
	 * @author luhuacong
	 * @date: 2018年12月13日
	 */
	@Override
	@Transactional
	public List<CampusBasicInfoTyVo> selectCampusListTy(String jxlmc) {
		List<CampusBasicInfoPo> list = null;
		try {
			if(jxlmc!=null && !"".equals(jxlmc)) {
				List<CampusBasicInfoTyVo> li = campusBasicInfoMapper.selectCampusByBuildId(jxlmc);
				return li;
			}else {
				list = campusBasicInfoMapper.selectList(null);
				
			}
			
			return BeanCopyUtils.copyListProperties(list, CampusBasicInfoTyVo.class);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("查询校区失败！", e);
		}
	}
}
